package com.wuseguang.springcloud.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class HaiweidaUtil {

    private static Logger logger = LoggerFactory.getLogger(HaiweidaUtil.class);

    //url公用部分
    private static String url = "http://hwdedu.mzapi.com:8067/apij/";
    //海威达的测试环境
    //private static String url = "http://edutest.mzapi.com:8077/apij";


    /**
     * 登录
     *
     * @param schoolCode
     * @param userName
     * @param password
     * @return
     */
    public static Map<String, Object> login(String schoolCode, String userName, String password) {
        //封装参数
        JSONObject json = new JSONObject();
        json.put("schoolCode", schoolCode);
        json.put("userName", userName);
        json.put("passWord", password);

        //登录不需要token,获取登录结果
        String result = HttpRequestUtil.sendPostToken(url + "login", json.toString(), "");
        logger.info(result);
        //转成json
        JSONObject jsonTwo = JSONObject.parseObject(result);

        try {
            //获取code
            String code = jsonTwo.get("code").toString();
            //判断code不等于登录成功
            if (!"2000".equals(code)) {
                //跑出一个错误
                throw new Exception("登录失败");
            }
            //获取登录结果
            JSONObject data = (JSONObject) jsonTwo.get("data");
            //赋值map
            Map<String, Object> map = new HashMap<String, Object>() {
                {
                    put("uuid", data.get("uuid"));
                    put("expTime", data.get("expTime"));
                    put("token", data.get("token"));
                }
            };
            return map;
        } catch (Exception e) {
            return null;
        }
    }



    /**
     * 刷新token
     *
     * @param uuid
     * @param token
     * @return
     */
    public static Map<String, Object> refreshToken(String uuid, String token) {
        //刷新token
        String result = HttpRequestUtil.sendPostToken(url + "access/RefreshToken?uuid=" + uuid, "", token);
        logger.info(result);
        //转成json
        JSONObject jsonTwo = JSONObject.parseObject(result);

        try {
            //获取code
            String code = jsonTwo.get("code").toString();
            //判断code不等于登录成功
            if (!"2000".equals(code)) {
                //跑出一个错误
                throw new Exception("无权限");
            }
            //获取登录结果
            JSONObject data = (JSONObject) jsonTwo.get("data");
            //赋值map
            Map<String, Object> map = new HashMap<String, Object>() {
                {
                    put("uuid", data.get("uuid"));
                    put("expTime", data.get("expTime"));
                    put("token", data.get("token"));
                }
            };
            return map;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取进出校信息
     *
     * @param token
     * @return
     */
    public static List<Map<String, Object>> haiweidaInAndOutOfSchool(String token,Integer minId) {

        logger.info("token = {}"+token);
        String result = HttpRequestUtil.GetTwo(url + "api/open/notice/gate/get?minId="+minId+"&size=1000", "", token);
        logger.info(result);
        //转成json
        JSONObject jsonTwo = JSONObject.parseObject(result);

        try {
            //获取code
            String code = jsonTwo.get("code").toString();
            //判断code不等于登录成功
            if (!"2000".equals(code)) {
                //跑出一个错误
                throw new Exception("无权限");
            }
            //获取登录结果
            List<Map<String, Object>> data = (List<Map<String, Object>>) jsonTwo.get("data");

            return data;

        } catch (Exception e) {
            List<Map<String, Object>> objects = new ArrayList<>();
            Map<String, Object> map = new HashMap<>();
            map.put("code","无权限");
            return objects;
        }
    }

    /**
     * 人脸信息同步
     *
     * @param json
     * @param token
     * @return
     */
    public static String faceSynchronization(String json, String token) {
        //登录不需要token,获取登录结果
        String result = HttpRequestUtil.sendPostToken(url + "api/open/basic/data_sync/photo", json, token);
        logger.info(result);
        //转成json
        JSONObject jsonTwo = JSONObject.parseObject(result);
        //返回数据
        return result;
    }

    /**
     * 用户信息同步
     *
     * @param json
     * @param token
     * @return
     */
    public static String userSynchronization(String json, String token) {
        //登录不需要token,获取登录结果
        String result = HttpRequestUtil.sendPostToken(url + "api/data_sync/users", json, token);

        logger.info(result);
        //转成json
        JSONObject jsonTwo = JSONObject.parseObject(result);
        //返回数据
        return result;
    }

    /**
     * 部门信息同步
     *
     * @param json
     * @param token
     * @return
     */
    public static String departmentSynchronization(String json, String token) {
        //登录不需要token,获取登录结果
        String result = HttpRequestUtil.sendPostToken(url + "api/data_sync/departments", json, token);
        logger.info(result);
        //转成json
        JSONObject jsonTwo = JSONObject.parseObject(result);
        //返回数据
        return result;
    }


    public static void main(String[] args) {

        Map<String, Object> login = login("20200827", "20200827", "123456");
        System.out.println(login.toString());
    }
}
